//
//  MakeSureTheOrderViewController.m
//  Edumap
//
//  Created by 辰领科技 on 16/7/20.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "MakeSureTheOrderViewController.h"
#import "SelectContactViewController.h"
#import "PayTypeViewController.h"

@interface MakeSureTheOrderViewController ()
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroview;
@property (strong, nonatomic) IBOutlet UIView *topView;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *phoneNumberLabel;
@property (strong, nonatomic) IBOutlet UILabel *addressLabel;

@property (strong, nonatomic) IBOutlet UIView *bottomView;
@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UILabel *moneyLabel;
@property (strong, nonatomic) IBOutlet UILabel *timesLabel;
@property (strong, nonatomic) IBOutlet UILabel *subjectsLabel;
@property (strong, nonatomic) IBOutlet UITextView *noteTextField;
@property (strong, nonatomic) IBOutlet UILabel *totalAmountLabel;

@property(nonatomic,strong)NSString * addressId;

@end

@implementation MakeSureTheOrderViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_isHaveAContact) {
        
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            self.count = 0;
        });
        _topView.hidden = NO;
        _bottomView.frame = flexibleFrame(CGRectMake(0, 115, 320, 285),NO);
        
    } else if (!_isHaveAContact) {
        
        _topView.hidden = YES;
        _bottomView.frame = flexibleFrame(CGRectMake(0, 44, 320, 285),NO);
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hadPay) name:@"hadPay" object:@"1"];
    [self initViewLayout];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.topView];
    [self adaptiveViewLayout:self.bottomView];
    [self adaptiveViewLayout:self.scroview];
    _scroview.contentSize = flexibleSize(CGSizeMake(0, 455), NO);
    
    self.noteTextField.layer.masksToBounds = YES;
    self.noteTextField.layer.cornerRadius = 5*VerticalRatio();
    self.noteTextField.layer.borderColor = [UIColor colorWithWhite:0.914 alpha:1.000].CGColor;
    self.noteTextField.layer.borderWidth = 1;
            
    if (_isHaveAContact) {
        
        _topView.hidden = NO;
        _bottomView.frame = flexibleFrame(CGRectMake(0, 115, 320, 285),NO);
        
    } else if (!_isHaveAContact) {
        
        _topView.hidden = YES;
        _bottomView.frame = flexibleFrame(CGRectMake(0, 44, 320, 285),NO);
        
    }
    
    
    if (_indentDic) {
        
        _timesLabel.text = [NSString stringWithFormat:@"x%ld",_num];
        
        CGFloat price = [[NSString stringWithFormat:@"%@",self.indentDic[@"mexpPrice"]]floatValue];
        _totalAmountLabel.text = [NSString stringWithFormat:@"￥%.2f",price*_num];
        _moneyLabel.text = [NSString stringWithFormat:@"￥%.2f/小时",[[NSString stringWithFormat:@"%@",self.indentDic[@"mexpPrice"]]floatValue]];
        _titleLabel.text = [NSString stringWithFormat:@"%@",self.indentDic[@"mexpName"]];
         _subjectsLabel.text = [NSString stringWithFormat:@"%@",self.indentDic[@"mallLecturerSubjects"][0][@"mlsuName"]];
        
        NSString * image = [NSString stringWithFormat:@"%@",_indentDic[@"mexpImage"]];
        if (image.length>0) {
            [_image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]];
        }
    }
    
    if (_courseDic) {
        _num = 1;
        _timesLabel.text = [NSString stringWithFormat:@"x%ld",_num];
        
        CGFloat price = [[NSString stringWithFormat:@"%@",self.courseDic[@"mgooPrice"]]floatValue];
        _totalAmountLabel.text = [NSString stringWithFormat:@"￥%.2f",price*_num];
        _moneyLabel.text = [NSString stringWithFormat:@"￥%.2f",[[NSString stringWithFormat:@"%@",self.courseDic[@"mgooPrice"]]floatValue]];
        _titleLabel.text = [NSString stringWithFormat:@"%@",self.courseDic[@"mgooName"]];
        _subjectsLabel.text = [NSString stringWithFormat:@"%@",self.courseDic[@"mgooContext"]];
        
        NSString * image = [NSString stringWithFormat:@"%@",_courseDic[@"mgooImage"]];
        if (image.length>0) {
            [_image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]];
        }
    }
    
}


- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark == 支付成功
- (void)hadPay{
    [self backBtn];
}

#pragma mark - 添加联系人
- (IBAction)add:(UIButton *)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeTeachStoryboard" bundle:nil];
    SelectContactViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"SelectContact"];
    
    page.count = self.count;
    page.choseAddress = ^(NSDictionary * dic){
         _addressId = dic[@"msadId"];
        _nameLabel.text = dic[@"msadReceiverName"];
        _phoneNumberLabel.text = dic[@"msadMobileNo"];
        _addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@",dic[@"msadProvinceName"],dic[@"msadCityName"],dic[@"msadAreaName"],dic[@"msadAddr"]];
        _addressLabel.numberOfLines = 0;
        [_addressLabel sizeToFit];
        
    };
    __weak typeof(self) weakSelf = self;
    page.SelectContactBlock = ^(BOOL isHaveAContact,NSInteger count) {
        weakSelf.isHaveAContact = isHaveAContact;
        weakSelf.count = count;
    };
    
    [[Common getNavigation] pushViewController:page];}

#pragma mark - 联系人
- (IBAction)contact:(UIButton *)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeTeachStoryboard" bundle:nil];
    SelectContactViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"SelectContact"];
    
    page.count = self.count;
    page.choseAddress = ^(NSDictionary * dic){
        _addressId = dic[@"msadId"];
        _nameLabel.text = dic[@"msadReceiverName"];
        _phoneNumberLabel.text = dic[@"msadMobileNo"];
        _addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@",dic[@"msadProvinceName"],dic[@"msadCityName"],dic[@"msadAreaName"],dic[@"msadAddr"]];
        _addressLabel.numberOfLines = 0;
        [_addressLabel sizeToFit];
    
    };
    __weak typeof(self) weakSelf = self;
    page.SelectContactBlock = ^(BOOL isHaveAContact,NSInteger count) {
        weakSelf.isHaveAContact = isHaveAContact;
        weakSelf.count = count;
    };
    
    [[Common getNavigation] pushViewController:page];
}

#pragma mark - 立即支付
- (IBAction)pay:(id)sender {
    
    if (!_addressId) {
        [CWHUDTips showTips:@"请添加联系人！"];
        return;
    }
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    [dic setObject:[NSString stringWithFormat:@"%ld",_num] forKey:@"modeNum"];
    
    [dic setObject:_noteTextField.text forKey:@"mordRemark"];
    
    self.shareApi.delegate=self;
    if (_indentDic) {
        [dic setObject:self.indentDic[@"mexpId"] forKey:@"mexpId"];
        [dic setObject:_addressId forKey:@"mallShippingAddressId"];
        [self.shareApi  requestOperationWithDic:dic withPath:JY_saveMallLectureOrder withHttpType:@"POST" withTag:JY_JY_saveMallLectureOrder];
    }
    
    if (_courseDic) {
        [dic setObject:self.courseDic[@"mgooId"] forKey:@"mgooId"];
        [dic setObject:_addressId forKey:@"mordAddressId"];
        [self.shareApi  requestOperationWithDic:dic withPath:JY_saveMallOrder withHttpType:@"POST" withTag:JY_JY_saveMallOrder];

    }
    

}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==JY_JY_saveMallLectureOrder) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
           [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeTeachStoryboard" bundle:nil];
            PayTypeViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PayType"];
                page.payOrderPrice = [NSString stringWithFormat:@"%@",database[@"result"][@"mlorPrice"]];
                page.payOrderName = [NSString stringWithFormat:@"%@",_indentDic[@"mexpName"]];
                page.payId = [NSString stringWithFormat:@"%@",database[@"result"][@"mlorNo"]];
                page.payOrderNo = [NSString stringWithFormat:@"%@",database[@"result"][@"mlorNo"]];
        
            [[Common getNavigation]pushViewController:page completion:^{
                [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
                    }];
                }

    }else if (tag == JY_JY_saveMallOrder){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeTeachStoryboard" bundle:nil];
            PayTypeViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PayType"];
            page.payOrderPrice = [NSString stringWithFormat:@"%@",database[@"result"][@"mordPrice"]];
            page.payOrderName = [NSString stringWithFormat:@"%@",_courseDic[@"mgooName"]];
            page.payId = [NSString stringWithFormat:@"%@",database[@"result"][@"mordNo"]];
            page.payOrderNo = [NSString stringWithFormat:@"%@",database[@"result"][@"mordNo"]];
            [[Common getNavigation]pushViewController:page completion:^{
                [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            }];
        }
        

    }
    
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    [self dismissWaitDialog];
}


@end
